package com.zs.ktsms.controller;

import com.zs.ktsms.pojo.Supplier;
import com.zs.ktsms.service.SupplierService;
import com.zs.ktsms.utils.Page;
import com.zs.ktsms.utils.ResponseMessage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/31 下午9:44
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService service;

    @GetMapping("/list")
    public ResponseMessage<Page<Supplier>> select(String supName,@RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        return ResponseMessage.success("供货商列表加载成功",service.getPage(supName,pageNo,pageSize));
    }

    @PostMapping("/add")
    public ResponseMessage add(@RequestBody Supplier supplier){
        int i = service.add(supplier);
        if(i>0){
            return ResponseMessage.success("添加成功");
        }else{
            return ResponseMessage.error("添加失败");
        }
    }

    @GetMapping("/detail")
    public ResponseMessage<Supplier> detail(Integer id){
        return ResponseMessage.success("供货商详情加载成功",service.detail(id));
    }

    @PostMapping("/update")
    public ResponseMessage update(@RequestBody Supplier supplier){
        int i = service.update(supplier);
        if(i>0){
            return ResponseMessage.success("修改成功");
        }else{
            return ResponseMessage.error("修改失败");
        }
    }

    @DeleteMapping("/del")
    public ResponseMessage delete(Integer id){
        int i = service.delete(id);
        if(i>0){
            return ResponseMessage.success("删除成功");
        }else{
            return ResponseMessage.error("删除失败");
        }
    }
}
